package com.bayleaf.dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class HSQLConnection {

	public static final String dbName = "hsqldb"; //db name
	public static final String dbPort = "9001"; 		// default port given

	/**
	 * 커넥션 얻어오기
	 * @return
	 * @throws IOException
	 */
    public static Connection getConnection(){
            Connection conn = null;
//            String dbUrl = "jdbc:hsqldb:http://70.121.45.136:" + dbPort + "/" + dbName;
//            String dbUrl = "jdbc:hsqldb:hsql://localhost:" + dbPort + "/" + dbName;
//            String dbUrl = "jdbc:hsqldb:file:../../db/" + dbName;
            String dbUrl = "jdbc:hsqldb:file:/data/project/swdc/bayleaf/webapp/webapps/bayleaf/db/hsqldb";
            try {
                    Class.forName("org.hsqldb.jdbcDriver");
                    conn = DriverManager.getConnection(dbUrl, "sa", "");
            } catch (Exception e) {
                    e.printStackTrace();
                    return null;
            }
//            System.out.println(HSQLConnection.class.getResource(".").getPath());
            return conn;
    }

    //Close Connection
    public static void close(Connection conn) {
            if (conn != null) {
                    try {
                            conn.close();
                    } catch (Exception e) {
                            e.printStackTrace();
                    }
            }
    }

    //Close Connection for PrepaparedStatment
    public static void close(PreparedStatement pStmt) {
            try {
                    if (pStmt != null) {
                            pStmt.close();
                            pStmt = null;
                    }
            } catch (SQLException e) {
                    e.printStackTrace();
            }
    }

    //Close Connection for ResultSet
    public static void close(ResultSet rs) {
            try {
                    if (rs != null) {
                            rs.close();
                            rs = null;
                    }
            } catch (SQLException e) {
                    e.printStackTrace();
            }
    }
}
